Email Using smtp

  • STEP 1 : MAIL CLIENT
  • STEP 2 : Coding

    1. ,env

    
                     MAIL_DRIVER=smtp
                    MAIL_HOST=mail.mydomain.in
                    MAIL_PORT=587
                    MAIL_USERNAME=info@mydomain.in
                    MAIL_PASSWORD=werwerwerwer
                    MAIL_ENCRYPTION=
                    MAIL_FROM_ADDRESS=info@mydomain.in
                    MAIL_TO_ADDRESS=info@mydomain.in
                    MAIL_BCC_ADDRESS=info@mydomain.in
                    MAIL_FROM_NAME="mydomain"
                    

    2. Mail class

    App\Mail\SendMail.php

    
                    namespace App\Mail;
    
    use Illuminate\Bus\Queueable;
    use Illuminate\Mail\Mailable;
    use Illuminate\Queue\SerializesModels;
    use Illuminate\Contracts\Queue\ShouldQueue;
    
    class SendMail extends Mailable
    {
        use Queueable, SerializesModels;
        public $data;
        /**
         * Create a new message instance.
         *
         * @return  void
         */
        public function __construct($data)
        {
            $this->data = $data;
        }
    
        /**
         * Build the message.
         *
         * @return  $this
         */
        public function build()
        {
            
                return $this->from(env('MAIL_FROM_ADDRESS'))->subject($this->data['subject'])->view($this->data['template'])->with($this->data);
           
            
        }
    }
    

    3. call the mail class in the controller

    
    use Illuminate\Support\Facades\Mail;
    use App\Mail\SendMail;
    
    
    
    $data['subject']  = "Admission Step1: Basic Info";
    $data['template']='emails.order_template';
    
    $data['name'] ="manoj";
    
    
    Mail::to(config('general.MAIL_TO_ADDRESS'))->bcc(config('general.MAIL_BCC_ADDRESS'))->send(new SendMail($data)); 
    

    1. emails.order_template is a blade file. Path is views/emails/order_template.blade.php

    2. we inject the array $data to the 'SendMail' class for using the data in the blade

    4. create mail template blade

    resources/views/emails/order_template.blade.php